/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.Page;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;
import com.jb.web.application.HtmlConstants;


public class InspectorLayoutManager extends RendererImpl {
    public static final StringBuffer INSPECTOR_HEADER = new StringBuffer()
    .append("<HTML>").append("<HEAD>")
        .append("<TITLE>Property Inspector </TITLE>")
        .append("<META http-equiv=Content-Type content=\"text/html; charset=utf-8\">")
        .append("<meta http-equiv=\"Pragma\" content=\"no-cache\"> ")
        .append("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/generic.css\" />\n")
    .append("<SCRIPT SRC=\"jslib/prototype.js\"  ></SCRIPT>\n")
    .append("<SCRIPT SRC=\"jslib/jb.core.js\"  ></SCRIPT>\n")
    .append("<SCRIPT SRC=\"jslib/jb.ajax.js\"  ></SCRIPT>\n")
    .append("<SCRIPT SRC=\"jslib/jb.inspector.js\" ></SCRIPT>\n")
         .append("</HEAD>");

    public StringBuffer renderBegin(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer stringBuffer = new StringBuffer();
        UIComponent selected = pCtx.getSession().getSelected();

        stringBuffer.append(HtmlConstants.DOC_TYPE).append(INSPECTOR_HEADER);
        if(selected!=null)
            stringBuffer
                .append("<BODY class=\"jbInspector\" onload=\"updateOpener('"+selected.getClientId(pCtx)+"')\">") //
                .append("<table width=\"100%\" ><tr><td style=\"background-color:#6699FF\">")
                    .append(selected.getClass().getName()  + "</td>").append("</tr><tr><td>");
        else {
            stringBuffer
                .append("<BODY class=\"jbInspector\" >") //
                .append("<table width=\"100%\" ><tr><td style=\"background-color:#6699FF\">")
                    .append("Not selected" + "</td>").append("</tr><tr><td>");
        }
        return stringBuffer;
    }

    public StringBuffer renderChildren(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer stringBuffer = new StringBuffer();
        UIComponent selected = pCtx.getSession().getSelected();
        if(selected==null)
            return stringBuffer;
        Page page = (Page) itemToRender;
        Iterator iter = page.getChildrenSorted(null).iterator();

        if (!iter.hasNext()) {
            stringBuffer.append("Please add component to this page");
        }

        while (iter.hasNext()) {
            UIComponentImpl aSect = (UIComponentImpl) iter.next();

            if (isVisible(pCtx, aSect)) {
                stringBuffer.append(aSect.render(pCtx));
            }
        }

        return stringBuffer;
    }

    public StringBuffer renderEnd(WebContext pCtx, UIComponent itemToRender) {
        return new StringBuffer("</td></tr></table></BODY></HTML>");
    }
}
